using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public static class ColorTools
    {
        private static readonly Dictionary<string, Color> map;

        static ColorTools()
        {
            var settings = LoadSettings();
            map = new Dictionary<string, Color>();
            for (var i = 0; i < settings.ColorGroups.Length; i++)
            {
                var group = settings.ColorGroups[i];
                for (var j = 0; j < group.list.Length; j ++)
                {
                    map.Add(FormatKey(group.type, group.list[j].key), group.list[j].value);
                }
            }
        }

        private static ColorSettings LoadSettings()
        {
            var loadedSettings = (ColorSettings)Resources.Load("ColorSettings", typeof(ColorSettings));
            if (loadedSettings == null)
            {
                loadedSettings = ScriptableObject.CreateInstance<ColorSettings>();
                Debug.LogError("ReplaceSettings is not exist (Resources)");
            }

            return loadedSettings;
        }

        private static string FormatKey(string type, string key)
        {
            return string.Format("{0}-{1}", type, key);
        }

        public static Color Get(string key)
        {
            if (map.ContainsKey(key)) return map[key];
            return Color.black;
        }

        public static Color Get(string type, string key)
        {
            return Get(FormatKey(type, key));
        }
    }
}